package constant.milk.periodapp.dialog;

import java.util.ArrayList;
import java.util.List;

import constant.milk.periodapp.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MilkListDialog extends Dialog
{
	private View contentView;
	
	private Context context;
	private TextView titleTextView;
	private ListView listView;
	
	private String title;
	
	private ListCallBack callBack;
	
	private ArrayList<String> list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_list);
		
		setFindView();
		setOnClick();
		setData();
	}

	private void setFindView()
	{
		contentView = findViewById(R.id.dialogListContentView);
		
		titleTextView = (TextView) findViewById(R.id.dialogListTitleTextView);
		listView = (ListView) findViewById(R.id.dialogListView);
	}

	private void setOnClick()
	{
		contentView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
			}
		});
	}

	private void setData()
	{
		titleTextView.setText(title);
		
		listView.setOnItemClickListener(oicl);
		
		DialogListAdapter adapter = new DialogListAdapter(context, R.layout.list_item_dialog, list);
		listView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	public MilkListDialog(Context context, String title, ListCallBack callBack, ArrayList<String> list)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		this.context = context;
		this.title = title;
		this.callBack = callBack;
		this.list = list;
	}
	
	private OnItemClickListener oicl = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			String data = (String) parent.getItemAtPosition(position);
			callBack.getData(data);
			dismiss();
		}
	};
	
	private class DialogListAdapter extends ArrayAdapter<String>
	{
		public DialogListAdapter(Context context, int resource,
				List<String> objects)
		{
			super(context, resource, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.list_item_dialog, null);
			}
			
			String data = getItem(position);
			
			TextView tv = (TextView) v.findViewById(R.id.listItemDialogTextView);
			
			tv.setText(data);
			
			return v;
		}
	}
	
	public interface ListCallBack
	{
		public abstract void getData(String data);
	}
}
